﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="../style/editor.css" rel="stylesheet" type="text/css">
    <script>
        var sLangDir = parent.oUtil.langDir;
        document.write("<scr" + "ipt src='language/" + sLangDir + "/websearch.js'></scr" + "ipt>");
    </script>
    <script>writeTitle()</script>

    <style type="text/css">
        #inpSearch, #inpReplace {
	        border:1px inset #ddd;
	        font-size:12px;
	        -moz-border-radius:3px; 
	        -webkit-border-radius:3px; 
	        padding-left:7px;
            }
    </style>

    <script language="javascript" type="text/javascript">
        var obj = parent.oUtil.obj;

        function doSearch() {
            var oEditor = parent.oUtil.oEditor;
            var oSel;
            if (navigator.appName.indexOf('Microsoft') != -1) {
                obj.setFocus();
                if (!obj.checkFocus()) { return; } //Focus stuff

                oSel = oEditor.document.selection.createRange();

                if (chkMatchCase.checked && chkMatchWord.checked) sType = 6;
                else if (chkMatchCase.checked) sType = 4;
                else if (chkMatchWord.checked) sType = 2;
                else sType = 0;
                oSel.collapse(false)
                if (oSel.findText(inpSearch.value, 1000000000, sType)) oSel.select();
                else {
                    if (confirm(getTxt("Finished searching"))) {
                        oSel.expand("textedit");
                        oSel.collapse("true");
                        oSel.select();
                        doSearch();
                    }
                }
                parent.oUtil.obj.bookmarkSelection();
                parent.realTime(parent.oUtil.oName);
            }
            else {

                var chkCase = document.getElementById("chkMatchCase").checked;
                var chkWord = document.getElementById("chkMatchWord").checked;
                var word = document.getElementById("inpSearch").value;

                oSel = oEditor.getSelection();
                oSel.collapseToEnd();
                if (oEditor.find(word, chkCase, false, true, chkWord, false, false)) {
                    //do nothing
                }
                else {
                    if (confirm(getTxt("Finished searching"))) {
                        oSel.collapse(oEditor.document.body, 0);
                        doSearch();
                    }
                }

                parent.realTime(parent.oUtil.oName);
            }

        }

        function doReplace() {
            var oEditor = parent.oUtil.oEditor;
            var oSel;
            if (navigator.appName.indexOf('Microsoft') != -1) {
                obj.setFocus();
                if (!obj.checkFocus()) { return; } //Focus stuff

                oSel = oEditor.document.selection.createRange();

                if (oSel.text.toLowerCase() == inpSearch.value.toLowerCase()) {
                    parent.oUtil.obj.saveForUndo();
                    oSel.text = inpReplace.value;
                    doSearch();
                }
                else {
                    doSearch();
                }
                parent.oUtil.obj.bookmarkSelection();
            }
            else {
                oSel = oEditor.getSelection();
                var range = oSel.getRangeAt(0);

                var word = document.getElementById("inpSearch").value;
                var repWord = document.getElementById("inpReplace").value;

                if (range.toString().toLowerCase() == word.toLowerCase()) {
                    parent.oUtil.obj.saveForUndo();
                    var repWordObj = oEditor.document.createTextNode(repWord);
                    range.deleteContents();
                    range.insertNode(repWordObj);
                    var newRange = oEditor.document.createRange();
                    newRange.selectNodeContents(repWordObj);
                    oSel.removeAllRanges();
                    oSel.addRange(newRange);
                    newRange.collapse(false);

                    doSearch();
                }
                else {
                    doSearch();
                }
            }

        }

        var nReplaced = 0;
        function doReplaceAll() {
            var oEditor = parent.oUtil.oEditor;
            var oSel;
            if (navigator.appName.indexOf('Microsoft') != -1) {
                obj.setFocus();
                if (!obj.checkFocus()) { return; } //Focus stuff

                oSel = oEditor.document.selection.createRange();

                if (chkMatchCase.checked && chkMatchWord.checked) sType = 6;
                else if (chkMatchCase.checked) sType = 4;
                else if (chkMatchWord.checked) sType = 2;
                else sType = 0;

                oSel.expand("textedit");
                oSel.collapse("true");
                oSel.select();

                if (oSel.findText(inpSearch.value, 1000000000, sType)) {
                    oSel.select();
                    oSel.text = inpReplace.value;
                    nReplaced++;
                    oSel.select();
                    doReplaceAll();
                }
                else {
                    alert(nReplaced + " occurrence(s) replaced.");
                    nReplaced = 0;
                    return;
                }
                obj.bookmarkSelection();
            }
            else {
                oSel = oEditor.getSelection();
                var range;

                var chkCase = document.getElementById("chkMatchCase").checked;
                var chkWord = document.getElementById("chkMatchWord").checked;
                var word = document.getElementById("inpSearch").value;
                var repWord = document.getElementById("inpReplace").value;

                var newRange = oEditor.document.createRange();
                newRange.selectNode(oEditor.document.body);
                newRange.collapse(true);
                oSel.removeAllRanges();
                oSel.addRange(newRange);

                while (true) {
                    if (oEditor.find(word, chkCase, false, true, chkWord, false, false)) {
                        var repWordObj = oEditor.document.createTextNode(repWord);
                        range = oEditor.getSelection().getRangeAt(0);
                        range.deleteContents();
                        range.insertNode(repWordObj);
                        newRange = oEditor.document.createRange();
                        newRange.selectNodeContents(repWordObj);
                        oSel.removeAllRanges();
                        oSel.addRange(newRange);
                        newRange.collapse(false);
                        nReplaced++;
                    }
                    else {
                        alert(nReplaced + " occurrence(s) replaced.");
                        nReplaced = 0;
                        return;
                    }
                }
            }

        }

        function doKeyUp() {
            if (document.getElementById("inpSearch").value == "") {
                document.getElementById("btnSearch").disabled = true;
                document.getElementById("btnReplace").disabled = true;
                document.getElementById("btnReplaceAll").disabled = true;
            }
            else {
                document.getElementById("btnSearch").disabled = false;
                document.getElementById("btnReplace").disabled = false;
                document.getElementById("btnReplaceAll").disabled = false;
            }
        }

    </script>

</head>
<body onload="loadTxt();document.getElementById('inpSearch').focus();" style="margin:12px;">

<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top" style="padding:5;">
    <table cellpadding="4" cellspacing="0">
    <tr>
        <td nowrap><span id="lblSearch">Search:</span></td>
        <td>
            <input type="text" id="inpSearch" name="inpSearch" onkeyup="doKeyUp()" style="width:150px;height:23px" class="inpTxt">
        </td>
        <td>
            <input type=button name=btnSearch id=btnSearch disabled value="search next" onclick="doSearch()" style="width:90px;height:30px" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
        </td>
    </tr>
    <tr>
        <td nowrap><span id="lblReplace">Replace:</span></td>
        <td>
            <input type="text" id="inpReplace" name="inpReplace" style="width:150px;height:23px" class="inpTxt">
        </td>
        <td>
            <input type=button name=btnReplace id=btnReplace disabled value="replace" onclick="doReplace()" style="width:90px;height:30px" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
        </td>
    </tr>
    <tr>
        <td nowrap colspan="2">
        <input type="checkbox" name="chkMatchCase" id="chkMatchCase" class="inpChk"> <label id="lblMatchCase" for="chkMatchCase">Match case</label>&nbsp;&nbsp;
        <span style="display:none"><input type="checkbox" name="chkMatchWord" id="chkMatchWord" class="inpChk"> <span id="lblMatchWhole">Match whole word</span></span>
        </td>
        <td>
            <input type=button name=btnReplaceAll id=btnReplaceAll disabled value="replace all" onclick="parent.oUtil.obj.saveForUndo();doReplaceAll()" style="width:90px;height:30px" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
        </td>
    </tr>
    </table>
</td>
</tr>
</table>

</body>
</html>
